<?php

class PXFDBInspector
{
	private function __construct()
	{
	}

	public static function getInstance()
	{
		static $ins= null;
		if(is_null($ins))
		{
			$ins = new PXFDBInspector();
		}
		return $ins;
	}

	public function getTable($table)
	{
		$db = PXFDataBase::getInstance();
		if(!PXFConfig::DB_INSPECTOR_CACHE || !$fields = PXFDBInspector::getCache($table))
		{
			$fields = array();
			$query = sprintf('SHOW COLUMNS FROM %s.%s',PXFConfig::DB_DB,$table);
			$result = $db->query($query);
			while($field = $db->fetch($result))
			{
				$name = $field['Field'];
				$fields[] = $name;
			}
			if(PXFConfig::DB_INSPECTOR_CACHE)
			{
				PXFDBInspector::putCache($table,$fields);
			}
		}
		return $fields;
	}

	private static function putCache($table,$structure)
	{
			return self::$__inspector_cache[$table] = $structure;
	}

	private static function getCache($table)
	{
			return (isset(self::$__inspector_cache[$table])) ? self::$__inspector_cache[$table] : null;
	}

	private static $__inspector_cache = array(
 	'session' => Array
                (
                    'id',
                    'hash',
                    'data',
                    'active',
                    'created',
                    'updated',
                    'destroyed',
                ),
            'user' => Array
                (
                    'id',
                    'name',
                    'lastname',
                    'pswd',
                    'mail',
                    'profile_id',
                    'currency',
                    'language',
                ),
            'category' => Array
                (
                    'id',
                    'title_es',
                    'title_en',
                ),
            'page' => Array
                (
                    'id',
                    'name',
                    'title',
                ),
            'product' => Array
                (
                    'id',
                    'title_es',
                    'title_en',
                    'content_es',
                    'content_en',
                    'price',
                    'image_id',
                    'posted_at',
                    'user_id',
                ),
            'image' => Array
                (
                    'id',
                    'title',
                    'product_id',
                    'thumbail',
                    'original',
                    'user_id',
                ),
            'paccess' => Array
                (
                    'id',
                    'profile_id',
                    'module',
                    'action',
                ),
            'post' => Array
                (
                    'id',
                    'title_en',
                    'title_es',
                    'content_en',
                    'content_es',
                    'image_id',
                    'comments',
                    'posted_at',
                    'user_id',
                ),
            'comment' => Array
                (
                    'id',
                    'post_id',
                    'content',
                    'posted_at',
                    'user_id',
                ),
            'corder' => Array
                (
                    'id',
                    'user_id',
                    'created',
                    'corder_status_id',
                ),
            'corder_status' => Array
                (
                    'id',
                    'title_en',
                    'title_es',
                ),
            'corder_item' => Array
                (
                    'id',
                    'product_id',
                    'quantity',
                    'price',
                    'corder_id',
                )
			);
}


?>
